package com.fwj.authorization.staff.controller;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fwj.authorization.staff.entity.SysButton;
import com.fwj.authorization.staff.entity.SysMenu;
import com.fwj.authorization.staff.service.SysButtonService;
import com.fwj.authorization.staff.service.SysMenuService;
import com.fwj.base.entity.R;
import com.fwj.base.exception.CustomException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * SysButtonController
 *
 * @author 樊温军
 * @date 2025/3/22 下午4:32
 */
@Slf4j
@RestController
@RequestMapping("/button")
@Api(tags = "按钮控制器")
@RequiredArgsConstructor
public class SysButtonController {

    private final SysButtonService sysButtonService;

    /**
     * 按钮—分页
     *
     * @param page    分页对象
     * @param sysButton 查询条件
     * @return R<IPage < SysButton>>
     */
    @ApiOperation(value = "按钮—分页")
    @GetMapping("/page")
    public R<IPage<SysButton>> getPage(Page<SysButton> page, SysButton sysButton) {
        try {
            log.info("按钮—分页，分页对象：{}，参数：{}", JSONUtil.toJsonStr(page), JSONUtil.toJsonStr(sysButton));
            IPage<SysButton> iPage = sysButtonService.getPage(page, sysButton);
            log.info("按钮—分页，返回结果：{}", JSONUtil.toJsonStr(iPage));
            return R.ok(iPage, "查询成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }


    /**
     * 按钮—保存
     *
     * @param sysButtons 按钮列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "按钮—保存")
    @PostMapping("/save")
    public R<Boolean> saveButton(@Valid @RequestBody List<SysButton> sysButtons) {
        try {
            log.info("按钮—保存，参数：{}", JSONUtil.toJsonStr(sysButtons));
            Boolean flag = sysButtonService.saveButton(sysButtons);
            log.info("按钮—保存，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

    /**
     * 按钮—批量删除
     *
     * @param ids 按钮ID列表
     * @return R<Boolean>
     */
    @ApiOperation(value = "按钮—批量删除")
    @PostMapping("/del")
    public R<Boolean> del(@Valid @RequestBody List<Long> ids) {
        try {
            log.info("按钮—批量删除，参数：{}", JSONUtil.toJsonStr(ids));
            Boolean flag = sysButtonService.removeByIds(ids);
            log.info("按钮—批量删除，返回结果：{}", flag);
            return R.ok(flag, "保存成功！");
        } catch (Exception e) {
            throw new CustomException(e);
        }
    }

}
